﻿@{
    ViewData["Title"] = "工作站添加";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新增工作站</title>
    <link href="//unpkg.com/layui@2.9.17/dist/css/layui.css" rel="stylesheet">
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="//unpkg.com/layui@2.9.17/dist/layui.js"></script>
    <script src="~/libs/jquery/jquery.js"></script>
</head>
<body>
    <form class="layui-form" action="" onsubmit="return false">
        <div class="layui-form-item">
            <label class="layui-form-label">工作站编码</label>
            <div class="layui-input-block" style="width:400px">
                <input type="text" name="workstationCode" id="workstationCode" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                <input type="checkbox" name="autoGenerate" lay-skin="switch" lay-text="自动生成" lay-filter="autoGenerate" style="display: inline-block; margin-left: 10px;">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">工作站名称</label>
            <div class="layui-input-block">
                <input type="text" name="workstationName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">工作站地点</label>
            <div class="layui-input-block">
                <input type="text" name="workstationAddress" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所在车间</label>
            <div class="layui-input-block">
                <select name="workshopId" lay-verify="required">
                    <option value="0">请选择</option>
                    <option value="3">车间1</option>
                    <option value="4">车间2</option>
                    <option value="5">车间3</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属工序</label>
            <div class="layui-input-block">
                <select name="processId" lay-verify="required">
                    <option value="0">请选择</option>
                    <option value="1">制造发动机</option>
                    <option value="2">制造机翼</option>
                    <option value="3">制造机身</option>
                    <option value="4">制造电池</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input type="radio" name="enableFlag" value="是" title="是" checked>
                <input type="radio" name="enableFlag" value="否" title="否">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" class="layui-textarea" name="remark"></textarea>
            </div>
        </div>
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
        </div>
    </form>
    <script type="text/javascript">
        layui.use('form', function () {
            const form = layui.form;
            form.render();
            // 生成随机代码
            function generateRandomCode() {
                return Math.random().toString(36).substr(2, 10).toUpperCase();
            }
            // 自动生成客户编码
            form.on('switch(autoGenerate)', function (data) {
                if (data.elem.checked) {
                    $('#workstationCode').val(generateRandomCode());
                } else {
                    $('#workstationCode').val('');
                }
            });
        });
    </script>

    <script>
        layui.use(function () {
            var $ = layui.$;
            var layer = layui.layer;
            var table = layui.table;
            var util = layui.util;
            var form = layui.form;
            // 对弹层中的表单进行初始化渲染
            form.render();
            // 表单提交事件
            form.on('submit(demo1)', function (data) {
                var field = data.field; // 获取表单字段值
                // 显示填写结果，仅作演示用
                // layer.alert(JSON.stringify(field), {
                //     title: '当前填写的字段值'
                // });
                var input = {
                    measureCode: field.MeasureCode,
                    measureName: field.measureName,
                    primaryFlag: field.primaryFlag,
                    primaryId: field.primaryId,
                    changeRate: field.changeRate,
                    enableFlag: field.enableFlag,
                    remark: field.remark,
                }
                // 此处可执行 Ajax 等操作
                // …
                $.ajax({
                    url: "/Li/AddWorkStation",
                    type: "get",
                    async: true,
                    data: field,
                    contentType: "application/json",
                    success: res => {
                        if (res != null) {
                            alert("添加成功")
                            var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的索引
                            parent.layer.close(index);//再执行关闭
                            return;
                        }
                        else {
                            console.log(res.code)
                            alert("添加失败")
                        }
                    }
                });
                //  console.log("1")
                return false; // 阻止默认 form 跳转
            });
        });
    </script>
</body>
</html>